package com.orhon.smartcampus.modules.base.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.DictionaryDao;
import com.orhon.smartcampus.modules.base.entity.Dictionary;
import com.orhon.smartcampus.modules.base.gqlModel.GQLDictionary;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.DictionaryMapper;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class DictionarysFetcher implements DataFetcher {

    @Autowired
    DictionaryDao dictionaryDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        List<GQLDictionary> dictionarys = new ArrayList<>();
        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
        String hql = "from Dictionary where deleted_at = null ";
        if(env.containsArgument("condition")) {
            HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
            if(map.get("id")!=null) {
                hql += "and id " + Util.decide(map.get("id"));
            }
            if(map.get("dictionary_value")!=null) {
                hql += "and dictionary_value " + Util.decide(map.get("dictionary_value"));
            }
            if(map.get("value")!=null) {
                hql += "and  value_code  " + Util.decide(map.get("value"));
            }
            if(map.get("name")!=null) {
                hql += "and dictionary_name "+ Util.decide(map.get("name"));
            }
            if(map.get("dictionary_code")!=null) {
                hql += "and dictionary_code " + Util.decide(map.get("dictionary_code"));
            }
            if(map.get("type")!=null) {
                hql += "and type  "+Util.decide(map.get("type"));
            }
            if(map.get("parent_id")!=null) {
                hql += "and parent_id  " +Util.decide(map.get("parent_id"));
            }
            if(map.get("alias")!=null) {
                hql += "and alias " + Util.decide(map.get("alias"));
            }
            if(map.get("status")!=null) {
                hql += "and status" + Util.decide(map.get("status"));
            }
        }
        
        List<Dictionary>  list = dictionaryDao.find(hql,null,page.getPage(),page.getLimit());
       
        for (Dictionary d : list) {
        	 GQLDictionary dictionary = DictionaryMapper.MAPPER.toDictionary(d);     
        	 dictionarys.add(dictionary);
        }
        Long total = 0L;
        if (env.getSelectionSet().contains("total")){
        	total = dictionaryDao.getTotalCount(hql);
        }
        
        return Result.ok().put("data", dictionarys).put("total", total);
    }
   

}
